package com.jdd.modules.form.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.AutoLog;
import com.jdd.modules.form.entity.FormParkTotalInfo;
import com.jdd.modules.form.service.IFormParkTotalInfoService;
import com.jdd.modules.form.util.FormParkTotalInfoListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 停车场每日统计表
 * @Author: jdd
 * @Date: 2021-03-12
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "停车场每日统计表")
@RestController
@RequestMapping("/form/ParkTotalInfo")
public class FormParkTotalInfoController {
    @Resource
    private IFormParkTotalInfoService iFormParkTotalInfoService;

    @Value("${parkInfo.parkCode}")
    private String parkCode;

    @AutoLog(value = "停车场每日统计表-根据停车场编码查询车流量echart数据")
    @ApiOperation(value = "停车场每日统计表-根据停车场编码查询车流量echart数据", notes = "停车场每日统计表-根据停车场编码查询车流量echart数据")
    @GetMapping(value = "/list")
    public Result<List<FormParkTotalInfo>> carVolumeChart(FormParkTotalInfo formParkTotalInfo, String startTime, String endTime) throws ParseException {
        Result<List<FormParkTotalInfo>> result = new Result<>();
        FormParkTotalInfoListUtil formParkTotalInfoListUtil = new FormParkTotalInfoListUtil();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        LambdaQueryWrapper<FormParkTotalInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(FormParkTotalInfo::getTime);
        int queryDays = 0;
        if (StringUtils.isNotBlank(endTime)) {
            queryDays = (int) ((simpleDateFormat.parse(endTime).getTime() - simpleDateFormat.parse(startTime).getTime()) / 1000 / 60 / 60 / 24) + 1;
        }
        if (StringUtils.isNotBlank(parkCode)) {
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                queryWrapper.between(FormParkTotalInfo::getTime, startTime, endTime);
                List<FormParkTotalInfo> list = iFormParkTotalInfoService.list(queryWrapper);
                result.setResult(formParkTotalInfoListUtil.listItemNullCheck(list, queryDays, endTime, parkCode));
            } else {
                queryWrapper.between(FormParkTotalInfo::getTime, simpleDateFormat.format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 14)), simpleDateFormat.format(new Date(System.currentTimeMillis())));
                List<FormParkTotalInfo> list = iFormParkTotalInfoService.list(queryWrapper);
                result.setResult(formParkTotalInfoListUtil.listItemNullCheck(list, 15, simpleDateFormat.format(new Date(System.currentTimeMillis())), parkCode));
            }
        } else {
            if (StringUtils.isNotBlank(formParkTotalInfo.getParkCode())) {
                if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                    queryWrapper.between(FormParkTotalInfo::getTime, startTime, endTime);
                    List<FormParkTotalInfo> list = iFormParkTotalInfoService.list(queryWrapper);
                    result.setResult(formParkTotalInfoListUtil.listItemNullCheck(list, queryDays, endTime, formParkTotalInfo.getParkCode()));
                } else {
                    queryWrapper.between(FormParkTotalInfo::getTime, simpleDateFormat.format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 14)), simpleDateFormat.format(new Date(System.currentTimeMillis())));
                    List<FormParkTotalInfo> list = iFormParkTotalInfoService.list(queryWrapper);
                    result.setResult(formParkTotalInfoListUtil.listItemNullCheck(list, 15, simpleDateFormat.format(new Date(System.currentTimeMillis())), formParkTotalInfo.getParkCode()));
                }
            } else {
                if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                    List<FormParkTotalInfo> list = iFormParkTotalInfoService.listNoParkCode(startTime, endTime);
                    result.setResult(formParkTotalInfoListUtil.listItemNullCheck(list, queryDays, endTime, null));
                } else {

                    List<FormParkTotalInfo> list = iFormParkTotalInfoService.listNoParkCode(simpleDateFormat.format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 14)), simpleDateFormat.format(new Date()));
                    result.setResult(formParkTotalInfoListUtil.listItemNullCheck(list, 15, simpleDateFormat.format(new Date(System.currentTimeMillis())), null));
                }
            }
        }
        return result;
    }
}
